package com.abel.bigwater.dataserver.model.qdies

import com.abel.bigwater.dataserver.model.PacketCmdException
import com.abel.bigwater.dataserver.model.PacketDataLenException
import io.netty.buffer.ByteBuf

/**
 * 读取D10与主站的上传模式
上位机发送： 帧头 地址 0x3A 0x00 校验 帧尾
D10应答： 帧头 地址 0x3A 0x01 D10与主站的上传模式 校验 帧尾
D10与主站的上传模式 1： 短信
2： GPRS
 */
class DtenAskUploadModeData(_len: Int) : D10BaseData(_len) {
    override var type = FuncType.AskUploadMode

    companion object {
        const val MIN_LEN = 1
    }

    var uploadMode = UploadModeType.GPRS

    override fun fill(buf: ByteBuf) {
        buf.writeByte(uploadMode.code)
    }

    override fun parse(buf: ByteBuf): Boolean {
        if (buf.readableBytes() < MIN_LEN) {
            throw PacketDataLenException("should contain upload mode.")
        }
        val b1 = buf.readByte()
        if (UploadModeType.values().find { it.code == b1.toInt() } == null) {
            throw PacketCmdException("upload mode invalid: $b1")
        }
        uploadMode = UploadModeType.values().find { it.code == b1.toInt() }!!

        return true
    }

    override fun count(): Int = 1
}

enum class UploadModeType(val code: Int) {
    SMS(0x01),

    GPRS(0x02)
}